// PartRod.cpp: implementation of the PartRod class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "PartRod.h"
#include "PartRodView.h"
#include "Dlgsupport.h"
#include "phsfactory.h"
#include "phsapp.h"
#include "PHSRecentWork.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

PartRod::PartRod()
{
	m_dbHeight = 0.0;
	m_dbBottomInsertLength = 0.0;
	m_dbTopInsertLength = 0.0;
	m_dbUnitWeight = 0.0;
	m_dbMinLength = 0.0;
	m_dbWeightPerMeter = 0.0;
	m_dbBottomScrewThreadLength = 0.0;
	m_dbTopScrewThreadLength = 0.0;

	SetStandard(GetRecentWork().ConnectStandard());
	SetTableName(this->GetCrudeTable());
}

PartRod::~PartRod()
{

}

BOOL PartRod::Accept(ModelVisitor* pVisitor)
{
	ASSERT(pVisitor);
	return pVisitor->Visit(*this);
}

DOUBLE PartRod::GetLength()
{
	return Part::GetLength() >= GetMinLength()?Part::GetLength():GetMinLength();
}

void PartRod::SetLength ( DOUBLE dLength )
{
	Part::SetLength(dLength >= GetMinLength()?dLength:GetMinLength());
}

void PartRod::SetMinLength(DOUBLE dbMinLength)
{
	ChangeStateInSetFunction(m_dbMinLength, dbMinLength);
	m_dbMinLength = dbMinLength;
	if(m_dbMinLength > Part::GetLength())
		Part::SetLength(m_dbMinLength);
}

void PartRod::SetBottomInsertLength(DOUBLE dbLen)
{
	GetVariableValue(_T("Size4"), m_dbBottomScrewThreadLength);
	if(dbLen <= m_dbBottomScrewThreadLength)
	{
		ChangeStateInSetFunction(m_dbBottomInsertLength, dbLen);
		m_dbBottomInsertLength = dbLen;
		SetVariableValue(_T("BEGININSERTLENGTH"), m_dbBottomInsertLength);
	}
}

void PartRod::SetTopInsertLength(DOUBLE dbLen)
{
	GetVariableValue(_T("Size3"), m_dbTopScrewThreadLength);
	if(dbLen <= m_dbTopScrewThreadLength)
	{
		ChangeStateInSetFunction(m_dbTopInsertLength, dbLen);
		m_dbTopInsertLength = dbLen;
		SetVariableValue(_T("ENDINSERTLENGTH"), m_dbTopInsertLength);
	}
}


void  PartRod::SetBottomScrewThreadLength(DOUBLE dbLength)
{
	m_dbBottomScrewThreadLength = dbLength;
	SetVariableValue(_T("size4"), dbLength);
	if(dbLength < m_dbBottomInsertLength)
		m_dbBottomInsertLength = dbLength;

	if(m_dbMinLength < m_dbBottomScrewThreadLength + m_dbTopScrewThreadLength)
		SetMinLength(m_dbBottomScrewThreadLength + m_dbTopScrewThreadLength);
}

void  PartRod::SetTopScrewThreadLength(DOUBLE dbLength)
{
	m_dbTopScrewThreadLength = dbLength;
	SetVariableValue(_T("size3"), dbLength);
	if(dbLength < m_dbTopInsertLength)
		m_dbTopInsertLength = dbLength;
	
	if(m_dbMinLength < m_dbBottomScrewThreadLength + m_dbTopScrewThreadLength)
		SetMinLength(m_dbBottomScrewThreadLength + m_dbTopScrewThreadLength);
}